package com.jiantong.web.open.app.version;

import com.google.common.collect.Maps;
import com.jiantong.version.model.DrugOrVaccine;
import com.jiantong.version.service.DrugOrVaccineReadService;
import com.jiantong.version.service.DrugOrVaccineWriteService;
import com.jiantong.web.core.enums.ExceptionCode;
import com.jiantong.work.enums.DocumentsDeleteFlag;
import io.terminus.boot.rpc.common.annotation.RpcConsumer;
import io.terminus.common.model.Paging;
import io.terminus.common.model.Response;
import io.terminus.pampas.openplatform.annotations.OpenBean;
import io.terminus.pampas.openplatform.annotations.OpenMethod;
import io.terminus.pampas.openplatform.exceptions.OPClientException;
import io.terminus.pampas.openplatform.exceptions.OPServerException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;
import java.util.Map;

/**
 * Created by mz on 7/9/17.
 */
@Slf4j
@OpenBean
public class DrugOrVaccineApis {

    @RpcConsumer
    private DrugOrVaccineReadService drugOrVaccineReadService;

    @RpcConsumer
    private DrugOrVaccineWriteService drugOrVaccineWriteService;

    @OpenMethod(
            key = "query.drug.or.vaccine.by.type",
            paramNames = {"type"},
            httpMethods = RequestMethod.GET
    )
    public List<DrugOrVaccine> queryListByType(Integer type) {
        if (type == null) {
            log.error("type must be not null");
            throw new OPClientException(ExceptionCode.PARAMETER_NOT_EXIST.value(), "type must be not null");
        }
        Map<String, Object> map = Maps.newHashMap();
        map.put("type", type);
        map.put("deleteFlag", DocumentsDeleteFlag.NORMAL.value());

        Response<List<DrugOrVaccine>> response = drugOrVaccineReadService.queryListByType(map);

        if (!response.isSuccess()) {
            log.error("failed to query list by type , case = {}", response.getError());
            throw new OPServerException(ExceptionCode.DATA_NOT_FOUND.value(), response.getError());
        }

        return response.getResult();
    }

    @OpenMethod(
            key = "paging.drug.or.vaccine",
            paramNames = {"pageNo","pageSize","type"},
            httpMethods = RequestMethod.GET
    )
    public Paging<DrugOrVaccine> paging(Integer pageNo, Integer pageSize, Integer type) {

        Response<Paging<DrugOrVaccine>> response = drugOrVaccineReadService.paging(pageNo, pageSize, type);

        if (!response.isSuccess()) {
            log.error("failed to paging drug or vaccine , case = {}", response.getError());
            throw new OPServerException(ExceptionCode.DATA_NOT_FOUND.value(),response.getError());
        }

        return response.getResult();
    }
}
